package com.thti.mecool.client.activity;
import java.util.Date;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.command.send.user.UserSend;
import com.thti.mecool.client.model.po.PlanClient;
import com.thti.mecool.client.model.po.Project;
import com.thti.mecool.client.model.po.Task;
import com.thti.mecool.client.protocol.MessageInfo;
import com.thti.mecool.client.util.RegexPattern;
import com.thti.mecool.client.util.StringUtil;
import com.thti.mecool.client.util.TimeUtil;
import com.thti.mecool.client.util.Util;
import com.thti.mecool.client.widget.Toolbar;

public class UpdatePasswdActivity extends BaseActivity {
	Button save,cancel;
	EditText oldPasswd,newPasswd,surePasswd;
	TextView currentUser;
	String oPasswd,nPasswd,nPasswdAgain;
	public static Handler handler;
	MessageInfo messageInfo;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.noTitle();
		setContentView(R.layout.mecool_change_secret);
		Cache.add(this);
		init();
		Toast.makeText(this, "您好,密码只允许字母,数字组成!", Toast.LENGTH_LONG).show();
		initToolbar();
		currentUser.setText(getCurrentUser().getLoginName());
		handler=new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				Bundle bundle=msg.getData();
				messageInfo=(MessageInfo) bundle.getSerializable("messageinfo");
				ischangePasswdSuccess(messageInfo);
			}
			
		};
		cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				oPasswd=oldPasswd.getText().toString();
//				nPasswd=newPasswd.getText().toString();
//				nPasswdAgain=surePasswd.getText().toString();
//				if(!BaseActivity.getCurrentUser().getPassword().equals(oPasswd)){
//					Toast.makeText(UpdatePasswdActivity.this, "旧密码输入不正确", 5000).show();
//				}
//				else{
//					if(nPasswd==null||nPasswd.equals("")){
//						Toast.makeText(UpdatePasswdActivity.this, "密码不能为空", 5000).show();
//					}else{
//						if(RegexPattern.isChars(nPasswd)){
//							Util.sendToast(UpdatePasswdActivity.this, "密码不能包括特殊字符");
//						}else{
//							if(!(nPasswd).equals(nPasswdAgain)){
//								Util.sendToast(UpdatePasswdActivity.this,  "输入两次新密码不一样");
//							}else{
//								UserSend.updateUser(BaseActivity.getCurrentUser().getLoginName(), oPasswd, nPasswd);
//							}
//						}
//					}
//				}
				registerPasswd();
			}
		});
		
	}
	private void init(){
		save=(Button)findViewById(R.id.mecool_change_secret_save);
		cancel=(Button)findViewById(R.id.mecool_change_secret_cancel);
		oldPasswd=(EditText)findViewById(R.id.mecool_change_secret_old);
		newPasswd=(EditText)findViewById(R.id.mecool_change_secret_new);
		surePasswd=(EditText)findViewById(R.id.mecool_change_secret_sure);
		currentUser=(TextView)findViewById(R.id.mecool_change_secret_currentuser);
	}	
	
	public void initToolbar(){
		Toolbar toolbar=new Toolbar(this);
		Toolbar.hideRightButton();
		toolbar.setToolbarCentreText("修改密码");
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
	}
	private OnClickListener leftbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			finish();
		}};
	
	public void ischangePasswdSuccess(MessageInfo messageInfo){
		if (messageInfo.getCode().equals("user002")){
			Toast.makeText(UpdatePasswdActivity.this, "用户名或者旧密码不正确", 5000).show();
		}else if (messageInfo.getCode().equals("common002")){
			Toast.makeText(UpdatePasswdActivity.this, "修改成功", 5000).show();
			SharedPreferences save_LoginInfo = getSharedPreferences("LoginPreferences", 0);
			Editor save_editor = save_LoginInfo.edit();
			save_editor.putString("userName", "null");
			save_editor.putString("passWord", "null");
			save_editor.putBoolean("autoLogin", false);
			save_editor.putBoolean("islogin", true);
			save_editor.commit();
			exitApp();
			Intent intent=new Intent(UpdatePasswdActivity.this,LoginActivity.class);
			startActivity(intent);
		}else if (messageInfo.getCode().equals("user001")){
			Toast.makeText(UpdatePasswdActivity.this, "新密码不能为空", 5000).show();
		}
		
	}	
	
	public void registerPasswd(){
		if(StringUtil.isBlank(oldPasswd.getText().toString())){
			oldPasswd.setError("密码不能为空！");
			return;
		}
		if(StringUtil.isBlank(newPasswd.getText().toString())){
			newPasswd.setError("密码不能为空！");
			return;
		}
		if(StringUtil.isBlank(surePasswd.getText().toString())){
			surePasswd.setError("密码不能为空！");
			return;
		}
		if(!BaseActivity.getCurrentUser().getPassword().equals(oldPasswd.getText().toString())){
			oldPasswd.setError("原密码输入错误!");
			return;
		}
		
		if(RegexPattern.isChars(newPasswd.getText().toString())){
			newPasswd.setError("请勿输入非法字符");
			return;	
		}
		if(RegexPattern.isChars(surePasswd.getText().toString())){
			surePasswd.setError("请勿输入非法字符");
			return;	
		}
		if(!newPasswd.getText().toString().equals(surePasswd.getText().toString())){
			Util.sendToast(this, "两次密码输入不一样");
			return;
		}
		
		UserSend.updateUser(BaseActivity.getCurrentUser().getLoginName(), oldPasswd.getText().toString(), newPasswd.getText().toString());
	}
		
	
	
	
	
		
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}	
		
		
}
